package com.ubix.kiosoft2;

import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import static com.ubix.kiosoft2.EventReader.EVENT_TYPE_SINGLE_TIME_SUCCESS;

/**
 * @Created by taylor on 9/27/18.
 */

public class ResultActivity extends AppCompatActivity {

    private TextView mResultView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_result);

        EventBus.getDefault().register(this);
        mResultView = (TextView) findViewById(R.id.tv_result);
        mResultView.setMovementMethod(ScrollingMovementMethod.getInstance());
    }

    public void onCopyResultToPasteBoard(View v) {

        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        cm.setText(mResultView.getText().toString().trim());
        Toast.makeText(this, "复制成功", Toast.LENGTH_SHORT).show();
    }

    // 主线程
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventReader event) {
        switch (event.eventType) {
            case EVENT_TYPE_SINGLE_TIME_SUCCESS:
                mResultView.setText("");
                break;
            default:
                String originalStr = mResultView.getText().toString().trim();
                mResultView.setText(originalStr + event.eventMsg);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}
